package com.zrwl.poscloud.core.restapi.admin.baseInfo;
import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.validator.group.Delete;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.commons.validator.group.Update;
import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.core.global.admin.DeptDataStatic;
import com.zrwl.poscloud.xo.service.AdminService;
import com.zrwl.poscloud.xo.service.DeptAdminRService;
import com.zrwl.poscloud.xo.vo.AdminVO;
import com.zrwl.poscloud.xo.vo.EmployeeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zrwl.poscloud.xo.service.GradeService;
import com.zrwl.poscloud.xo.vo.GradeVO;
/**
 * 班级 controller api类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-02-27
 */
@RestController
@Api(value = "班级相关接口", tags = {"班级相关接口"})
@RequestMapping("/grade")
@Slf4j
public class GradeRestApi {
    @Autowired
    private GradeService gradeService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private DeptAdminRService deptAdminRService;
    @AuthorityVerify
    @ApiOperation(value = "获取班级列表", notes = "获取班级列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody GradeVO gradeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取班级列表: {}", gradeVO);
        gradeVO.setDeptRoleTree(deptAdminRService.getMyDeptRoleTreeCode());
        return ResultUtil.successWithData(gradeService.getPageList(gradeVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取班级列表", notes = "获取班级列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody GradeVO gradeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取班级列表: {}", gradeVO);
        gradeVO.setDeptRoleTree(deptAdminRService.getMyDeptRoleTreeCode());
        if(gradeVO.getStatus() == 0){
            gradeVO.setStatus(1);
        }
        return ResultUtil.successWithData(gradeService.getAllList(gradeVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "新增班级")    @ApiOperation(value = "新增班级", notes = "新增班级", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody GradeVO gradeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("新增班级: {}", gradeVO);
        AdminVO adminVO = adminService.getMe();
        if (!adminVO.isCompany()) {
            ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        try {
            DeptDataStatic.DeptData deptData= DeptDataStatic.companyMap.get(adminVO.getCompanyUid());
            if(deptData!=null){
                deptData = null;
            }
            return gradeService.add(gradeVO,adminService.getMe().getUid());
        } catch (Exception e) {
            return ResultUtil.errorWithMessage(e.getMessage());
        }
    }

    @AuthorityVerify
    @OperationLogger(value = "修改班级")    @ApiOperation(value = "修改班级", notes = "修改班级", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody GradeVO gradeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("修改班级: {}", gradeVO);
        return gradeService.edit(gradeVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "部门替换为班级")    @ApiOperation(value = "部门替换为班级", notes = "部门替换为班级", response = String.class)
    @PostMapping("/addClassByDept")
    public String addClassByDept(@Validated({Insert.class}) @RequestBody GradeVO gradeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("部门替换为班级: {}", gradeVO);
        return gradeService.addClassByDept(gradeVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "班级毕业")    @ApiOperation(value = "班级毕业", notes = "班级毕业", response = String.class)
    @PostMapping("/editLeave")
    public String editLeave(@Validated({Update.class}) @RequestBody GradeVO gradeVO, BindingResult result) {
        if(gradeVO.getGradeVOList().size() == 0){
            ThrowableUtils.checkParamArgument(result);
        }
        AdminVO adminVO = adminService.getMe();
        log.info("班级毕业: {}", gradeVO);
        return gradeService.editLeave(gradeVO,adminVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "删除班级和部门")    @ApiOperation(value = "删除班级和部门", notes = "删除班级和部门", response = String.class)
    @PostMapping("/delClassAndDept")
    public String delClassAndDept(@Validated({Delete.class}) @RequestBody GradeVO gradeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("删除班级和部门: {}", gradeVO);
        return gradeService.delClassAndDept(gradeVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "删除班级")    @ApiOperation(value = "删除班级", notes = "删除班级", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody GradeVO gradeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("删除班级: {}", gradeVO);
        return gradeService.delete(gradeVO);
    }

    @AuthorityVerify
    @ApiOperation(value = "获取班级列表", notes = "获取班级列表", response = String.class)
    @PostMapping("/getGradeAllList")
    public String getGradeAllList(@Validated({GetList.class}) @RequestBody GradeVO gradeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取班级列表: {}", gradeVO);
//        gradeVO.setDeptRoleTree(deptAdminRService.getMyDeptRoleTreeCode());
        gradeVO.setTeacherType(-1);
        return ResultUtil.successWithData(gradeService.getAllList(gradeVO));
    }
}
